package miaofusdk

import (
	"encoding/json"
	"errors"
	"fmt"

	"gitee.com/garlic/tools"
)

type ShopBean struct {
	Id                         int    `json:"id,omitempty,string"`
	WxId                       string `json:"wx_id,omitempty"`
	AliId                      string `json:"ali_id,omitempty"`
	UnionId                    string `json:"union_id,omitempty"`
	Name                       string `json:"name,omitempty"`
	NameAbbr                   string `json:"name_abbr,omitempty"`
	Creater                    string `json:"creater,omitempty"`
	Code                       string `json:"code,omitempty"`
	Address                    string `json:"address,omitempty"`
	ContactName                string `json:"contact_name,omitempty"`
	ContactNumber              string `json:"contact_number,omitempty"`
	ContactEmail               string `json:"contace_email,omitempty"`
	License                    string `json:"license,omitempty"`
	LicenceCode                string `json:"license_code,omitempty"`
	LicenceName                string `json:"license_name,omitempty"`
	LicenceExpires             string `json:"license_expires,omitempty"`
	AccountName                string `json:"account_name,omitempty"`
	AccountBank                string `json:"account_bank,omitempty"`
	AccountCard                string `json:"account_card,omitempty"`
	LegalPersonName            string `json:"legal_person_name,omitempty"`
	LegalPersonId              string `json:"legal_person_id,omitempty"`
	LegalPersonIdUrlFront      string `json:"legal_person_id_url_front,omitempty"`
	LegalPersonIdUrlBack       string `json:"legal_person_id_url_back,omitempty"`
	UserId                     int    `json:"user_id,omitempty,string"`
	OrgId                      int    `json:"org_id,omitempty,string"`
	RegisterId                 string `json:"register_id,omitempty"`
	AliCategoryId              string `json:"ali_category_id,omitempty"`
	BusinessCertificate        string `json:"business_certificate,omitempty"`
	BusinessCertificateExpires string `json:"business_certificate_expires,omitempty"`
	AuthLetter                 string `json:"auth_letter,omitempty"`
	OtherAuthorization         string `json:"other_authorization,omitempty"`
	WxNotifyUrl                string `json:"wx_notify_url,omitempty"`
	UseDownTradeNo             int    `json:"use_down_tradeno,omitempty"`
	Iso2o                      bool   `json:"iso2o,omitempty"`
}

type RespShop struct {
	Response
	Data ShopBean `json:"data,omitempty"`
}

type RespShops struct {
	Response
	Page       int        `json:"page,string"`
	Rows       int        `json:"rows,string"`
	TotalCount int        `json:"total_count,string"`
	TotalPage  int        `json:"total_page,string"`
	List       []ShopBean `json:"list,omitempty,string"`
}

//获取商户信息
func (s *SrvShopManager) GetShopById(id int) (ShopBean, error) {
	resp := RespShop{}
	http_request := s.GetPost(s.GetUrl() + "/shop/query")
	http_request.Param("shop_id", fmt.Sprintf("%d", id))
	if v, err := http_request.Bytes(); err != nil {
		return resp.Data, err
	} else if err := json.Unmarshal(v, &resp); err != nil {
		return resp.Data, err
	} else if resp.Code != "0" {
		return resp.Data, errors.New(fmt.Sprintf("%s:%s", resp.Code, resp.Msg))
	} else {
		return resp.Data, nil
	}
}

func (s *SrvShopManager) GetShopByCode(code string) (ShopBean, error) {
	resp := RespShop{}
	http_request := s.GetPost(s.GetUrl() + "/shop/query")
	http_request.Param("code", code)
	if v, err := http_request.Bytes(); err != nil {
		return resp.Data, err
	} else if err := json.Unmarshal(v, &resp); err != nil {
		return resp.Data, err
	} else if resp.Code != "0" {
		return resp.Data, errors.New(resp.Msg)
	} else {
		return resp.Data, nil
	}
}

//通过 pid 获取商户信息
func (s *SrvShopManager) GetShopByPid(pid string) (ShopBean, error) {
	resp := RespShop{}
	http_request := s.GetPost(s.GetUrl() + "/shop/query")
	http_request.Param("pid", pid)
	if v, err := http_request.Bytes(); err != nil {
		return resp.Data, err
	} else if err := json.Unmarshal(v, &resp); err != nil {
		return resp.Data, err
	} else if resp.Code != "0" {
		return resp.Data, errors.New(fmt.Sprintf("%s:%s", resp.Code, resp.Msg))
	} else {
		return resp.Data, nil
	}
}

func (s *SrvShopManager) UpdateShop(shop ShopBean) (ShopBean, error) {
	resp := RespShop{}
	http_request := s.GetPost(s.GetUrl() + "/shop/update")
	m := tools.JsonToMap(shop)
	for k, _ := range m {
		value := fmt.Sprintf("%v", m[k])
		if value != "" {
			http_request.Param(k, value)
		}
	}
	if v, err := http_request.Bytes(); err != nil {
		return resp.Data, err
	} else if err := json.Unmarshal(v, &resp); err != nil {
		return resp.Data, err
	} else if resp.Code != "0" {
		return resp.Data, errors.New(resp.Msg)
	} else {
		return resp.Data, nil
	}
}

func (s *SrvShopManager) ShopList(page, rows, org_id int, query_type, query_value string) (RespShops, error) {
	resp := RespShops{}
	http_request := s.GetPost(s.GetUrl() + "/shop/list")
	http_request.Param("page", fmt.Sprintf("%d", page))
	http_request.Param("rows", fmt.Sprintf("%d", rows))
	http_request.Param("org_id", fmt.Sprintf("%d", org_id))
	http_request.Param("query_type", query_type)
	http_request.Param("query_value", query_value)
	if v, err := http_request.Bytes(); err != nil {
		return resp, err
	} else if err := json.Unmarshal(v, &resp); err != nil {
		return resp, err
	} else {
		return resp, nil
	}
}
